# NB: see http://inkvine.fluff.org/~chris/arm-tools.html for a set of instructions
# plus e-mails on linux-arm, nov 7-8.
# (which I did not follow literally)

## directories
GNUSRCDIR=/share/downloads/gnu
LINUX7KSRCDIR=/share/downloads/linux7k
XDEVNAME=xdev
TARGETDIR=/scratch/psion/$XDEVNAME

##  version numbers (=directory names)
LINUXVER=linux-2.2.1
BINUTILVER=binutils-2.9.5.0.19
GLIBCVNO=2.1.2
GCCCOREVER=2.95.2

## files
# Linux/ARM 
LINUXSRC=$LINUX7KSRCDIR/$LINUXVER.tar.gz
PATCHRMK2=$LINUX7KSRCDIR/patch-2.2.1-rmk2.gz
PATCHPHILB=$LINUX7KSRCDIR/linux-2.2.1-philb-990208.gz
GCCARMPATCH=$LINUX7KSRCDIR/gcc-2.95.2-diff-991022.gz  

# linux7k
CBPATCH=$LINUX7KSRCDIR/crash+burn-26.patch.gz

# gnu stuff
GLIBCVER=glibc-$GLIBCVNO
BINUTILSRC=$GNUSRCDIR/$BINUTILVER.tar.bz2
GLIBCSRC=$GNUSRCDIR/$GLIBCVER.tar.gz
GLIBCTHREADS=$GNUSRCDIR/glibc-linuxthreads-$GLIBCVNO.tar.gz
GLIBCCRYPT=$GNUSRCDIR/glibc-crypt-$GLIBCVNO.tar.gz
GCCCORESRC=$GNUSRCDIR/gcc-core-$GCCCOREVER.tar.gz


## clean old stuff
cd $TARGETDIR
cd ..
rm -rf $XDEVNAME
mkdir $XDEVNAME

## setup directories
cd $TARGETDIR
mkdir src

## install binutils
cd $TARGETDIR
cd src
bunzip2 -c $BINUTILSRC | tar -x
cd $BINUTILVER
./configure --target=arm-linux --prefix=$TARGETDIR 2>&1 | tee ../binutilconf.log
make   2>&1 | tee ../binutilmake.log
make install  2>&1 | tee ../binutilmakeinstall.log
# check that new binaries are in $TARGETDIR/bin
ls -l  $TARGETDIR/bin 
PATH=$PATH:$TARGETDIR/bin
# get rid of source tree
cd .. 
rm -rf $BINUTILVER

## install linux kernel source
cd $TARGETDIR
cd src
mkdir $LINUXVER
ln -s  $LINUXVER linux
tar -xzf $LINUXSRC 
gunzip -c  $PATCHRMK2 | patch -p0 
gunzip -c  $PATCHPHILB | patch -p0
cd linux
gunzip -c $CBPATCH | patch -p1 
make symlinks
cp myconfig .config
make oldconfig 
# yes to Matrox  and ATI Mach
make dep

## link in  kernel header files for gcc
cd $TARGETDIR
mkdir arm-linux/include
# copy relevant kernel header files, then remove rest of source tree
cp -r $TARGETDIR/src/linux/include/linux  arm-linux/include
cp -r $TARGETDIR/src/linux/include/asm  arm-linux/include
cd src 
rm -rf $LINUXVER
rm linux



##  patch and build gcc cross compiler, first without libc header files
cd $TARGETDIR
cd src
tar xfz $GCCCORESRC
cd gcc-$GCCCOREVER
gunzip -c $GCCARMPATCH | patch -p0 2>&1 | tee ../gccpat.log     
# -Dinhibit_libc hack
#
#Upon installing a successful build of gcc, some headers will get put in the
#target's include directory. However, if you are building a (cross) compiler
#for the very first time, or with a different set of paths, it won't have
#these headers to hand. For the first time you build a gcc, then, you can
#follow through these steps to fix the problem:
#
#
#Edit gcc/config/arm/t-linux, and add -Dinhibit_libc and -D__gthr_posix_h to
#TARGET_LIBGCC2_CFLAGS.
cd $TARGETDIR/src/gcc-$GCCCOREVER/gcc/config/arm
joe t-linux
cd $TARGETDIR/src/gcc-$GCCCOREVER
 ./configure --target=arm-linux --prefix=$TARGETDIR --disable-threads  2>&1 | tee ../gcc-coreconf.log
make LANGUAGES="c"  2>&1 | tee ../gcc-coremake.log
make LANGUAGES="c" install  2>&1 | tee ../gcc-coremakeinstall.log  
# check that arm-linux-gcc is available
ls -l $TARGETDIR/bin/arm-linux-gcc
# remove source tree
cd ..
rm -rf gcc-$GCCCOREVER 

## cross-compile glibc 
# configure of glibc needs to know that the cross compiler is the default
CC=arm-linux-gcc
cd $TARGETDIR
cd src
tar -xzf $GLIBCSRC
cd $GLIBCVER
tar -xzf $GLIBCTHREADS
tar -xzf $GLIBCCRYPT
cd ..
mkdir $GLIBCVER-build
cd  $GLIBCVER-build
../$GLIBCVER/configure  arm-linux --prefix=$TARGETDIR/arm-linux \
 --build=i386-linux \
 --enable-add-ons  2>&1 | tee ../glibcconf.log  
make  2>&1 | tee ../glibcmake.log   
# takes about 40 minutes or so
make install  2>&1 | tee ../glibcmakeinstall.log   
# takes about 5 minutes
# check that header files are in $TARGETDIR/arm-linux/include
ls -l $TARGETDIR/arm-linux/include 
# remove source tree etc.
cd ..
rm -rf $GLIBCVER
rm -rf $GLIBCVER-build

## rebuild the cross-compiler, now the libc header files should be included
cd $TARGETDIR
cd src
tar xfz $GCCCORESRC
cd gcc-$GCCCOREVER
gunzip -c $GCCARMPATCH | patch -p0 2>&1 | tee ../gccpat.log     
 ./configure --target=arm-linux --prefix=$TARGETDIR   2>&1 | tee ../gcc-core2conf.log
make LANGUAGES="c"  2>&1 | tee ../gcc-core2make.log
make LANGUAGES="c" install  2>&1 | tee ../gcc-core2makeinstall.log  
# check that arm-linux-gcc is available
ls -l $TARGETDIR/bin/arm-linux-gcc
# remove source tree
cd ..
rm -rf gcc-$GCCCOREVER


## cross-compile glibc again with new cross compiler
cd $TARGETDIR
cd src
rm -rf $GLIBCVER
tar -xzf $GLIBCSRC
cd $GLIBCVER
tar -xzf $GLIBCTHREADS
tar -xzf $GLIBCCRYPT
cd ..
rm -rf $GLIBCVER-build   
mkdir $GLIBCVER-build
cd  $GLIBCVER-build
../$GLIBCVER/configure  arm-linux --prefix=$TARGETDIR/arm-linux \
 --build=i386-linux \
 --enable-add-ons  2>&1 > ../glibc2conf.log  
make  2>&1 > ../glibc2make.log   
make install  2>&1 > ../glibc2makeinstall.log   
ls -l $TARGETDIR/lib   
cd ..
rm -rf $GLIBCVER
rm -rf $GLIBCVER-build
cd ..
tar -czvf crossgcclog991108.tgz src
cd src
rm *
